package cn.com.guage.flink.transformation;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * 
 * @author yangdechao flatMap算子用法
 *
 */
public class FlatMapTransformation {

	public static void main(String[] args) throws Exception {
		StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

		DataStreamSource<String> textStream = env.readTextFile("data/flatmap-data.txt");
		//SingleOutputStreamOperator<String> resuilt = textStream.flatMap(new MyFlatMap());
		DataStream<Tuple2<String,Integer>> resuilt = textStream.flatMap(new MyFlatMap2());

		resuilt.print();
		env.execute();
	}

	public static final class MyFlatMap implements FlatMapFunction<String, String> {
		/**
		 * 
		 */
		private static final long serialVersionUID = -3036112274448895746L;

		public void flatMap(String value, Collector<String> out) throws Exception {
			String[] words = value.split(" ");
			for (String word : words) {
				out.collect(word);
			}
		}
	}
	
	public static final class MyFlatMap2 implements FlatMapFunction<String,Tuple2<String,Integer>> {
		/**
		 * 
		 */
		private static final long serialVersionUID = -3036112274448895746L;

		public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
			String[] words =  value.split(" ");
			for (String word : words) {
				out.collect(new Tuple2<String, Integer>(word, 1));
			}
		}
	
	}
}
